package com.lgrglw.common.page;

import java.util.LinkedList;
import java.util.List;

public class Page<T> {
	/**
	 * 结果集
	 */
	private List<T> result;
	/**
	 * 当前第几页从1开始，如果没有记录则为0
	 */
	private Integer currentNo;
	/**
	 * 总页数无记录时为0
	 */
	private Integer totalNo;
	/**
	 * 每页数据条数
	 */
	private Integer pageSize;
	/**
	 * 上一页记录
	 */
	private Integer proNo;
	/**
	 * 下一页记录
	 */
	private Integer nextNo;
	/**
	 * 符合条件的总记录数
	 */
	private Integer totalSize;
	/**
	 * 自动分页处理
	 * @param list 当前页内容
	 * @param pageNo 当前页号
	 * @param total 总记录数
	 * @param size 每页记录数
	 */
	public Page(List<T> list, int pageNo, int total, int size) {
		if (list.size() > 0) {
			this.result = list;
			this.currentNo = pageNo;
			this.totalSize = total;//符合条件的记录数
			this.pageSize=size;
			this.js(total, size, pageNo);
		} else {
			result = new LinkedList<T>();
			this.totalNo = 0;
			this.currentNo = 1;
			this.nextNo = 1;
			this.proNo = 1;
			this.pageSize = size;
		}
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public Integer getCurrentNo() {
		return currentNo;
	}

	public void setCurrentNo(Integer currentNo) {
		this.currentNo = currentNo;
	}

	public Integer getTotalNo() {
		return totalNo;
	}

	public void setTotalNo(Integer totalNo) {
		this.totalNo = totalNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getProNo() {
		return proNo;
	}

	public void setProNo(Integer proNo) {
		this.proNo = proNo;
	}

	public Integer getNextNo() {
		return nextNo;
	}

	public void setNextNo(Integer nextNo) {
		this.nextNo = nextNo;
	}

	public Integer getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(Integer totalSize) {
		this.totalSize = totalSize;
	}
	/**
	 * 计算总页数，上一页，下一页的内容
	 * @param total
	 * @param pageSize
	 * @param current
	 */
	private void js(int total,int pageSize,int current){
		if(total%pageSize==0){
			this.totalNo=total/pageSize;
		}else{
			this.totalNo=(total/pageSize)+1;
		}
		if(current<=1){
			this.proNo=1;
			if(totalNo>1){
				this.nextNo=2;
			}else{
				this.nextNo=1;
			}
		}else{
			this.proNo=current-1;//上一页
			if(current>=totalNo){
				this.nextNo=current;//没有最好一页了
			}else{
				this.nextNo=current-1;
			}
		}
	}
}
